<template>
  <div class="login-container">
    <!-- 登录卡片 -->
    <div class="login-card">
      <div class="login-header">
        <div class="logo">
          <i class="el-icon-s-platform"></i>
        </div>
        <h1 class="title">后台管理系统</h1>
        <p class="subtitle">欢迎回来，请登录您的账户</p>
      </div>
      
      <el-form 
        :model="loginForm" 
        :rules="loginRules" 
        ref="loginFormRef" 
        class="login-form"
        @keyup.enter="handleLogin"
      >
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入用户名"
            size="large"
            class="login-input"
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码"
            size="large"
            class="login-input"
            show-password
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <div class="form-options">
          <el-checkbox v-model="rememberMe">记住我</el-checkbox>
          <a href="#" class="forgot-password">忘记密码？</a>
        </div>
        
        <el-form-item>
          <el-button 
            type="primary" 
            @click="handleLogin" 
            class="login-button"
            size="large"
            :loading="loading"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
        
        <div class="register-link">
          <span>还没有账户？</span>
          <router-link to="/register" class="link">立即注册</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { authApi } from '@/api'
import { User, Lock } from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()

const loginForm = ref({
  username: '',
  password: '',
})

const rememberMe = ref(false)
const loading = ref(false)
const loginFormRef = ref(null)

// 登陆时用户名，密码格式校验
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, message: '用户名至少3个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少6个字符', trigger: 'blur' }
  ],
}

// 登录按钮
const handleLogin = async () => {
  console.log("loginFormRef.value: ",loginFormRef.value)
  if (!loginFormRef.value) return
  try {
    const valid = await loginFormRef.value.validate()
    console.log("valid: ", valid)
    if (valid) {
      loading.value = true

      // 调用真实的登录API
      const response = await authApi.login({
        account: loginForm.value.username,
        password: loginForm.value.password
      })
      
      console.log('登录响应:', response)
      console.log('响应类型:', typeof response)
      console.log('响应字段:', Object.keys(response))
      
      // 登录成功，更新用户状态
      await userStore.setLoginState(response.data)
      
      // 记住登录状态
      if (rememberMe.value) {
        localStorage.setItem('rememberMe', 'true')
        localStorage.setItem('savedUsername', loginForm.value.username)
      } else {
        localStorage.removeItem('rememberMe')
        localStorage.removeItem('savedUsername')
      }
      
      ElMessage.success('登录成功！')
      loading.value = false
      
      // 跳转到仪表板
      router.push('/dashboard')
    }
  } catch (error) {
    loading.value = false
    console.error('登录错误详情:', error)
    ElMessage.error('登录失败：' + (error.response?.data?.message || error.message))
  }
}
// 页面加载时检查是否有保存的登录信息
const initSavedLogin = () => {
  const savedRememberMe = localStorage.getItem('rememberMe')
  const savedUsername = localStorage.getItem('savedUsername')
  
  if (savedRememberMe === 'true' && savedUsername) {
    loginForm.value.username = savedUsername
    rememberMe.value = true
  }
}
// 页面挂载时初始化
initSavedLogin()
</script>

<style scoped>
.login-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100vw;
  height: 100vh;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-y: auto;
  z-index: 9999;
}

/* 禁用滚动条 */
.login-container::-webkit-scrollbar {
  display: none;
}
.login-container {
  -ms-overflow-style: none;
  scrollbar-width: none;
}

:global(body) {
  overflow: hidden !important;
  background: #fff !important;
}

.login-card {
  background: transparent;
  border-radius: 0;
  padding: 60px 0 40px 0;
  width: 100%;
  max-width: 420px;
  box-shadow: none;
  border: none;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin: 0 auto;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  width: 80px;
  height: 80px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 30px;
  font-size: 32px;
  color: white;
  box-shadow: 0 15px 30px rgba(102, 126, 234, 0.3);
}

.title {
  font-size: 36px;
  font-weight: 700;
  color: #2c3e50;
  margin-bottom: 12px;
}

.subtitle {
  color: #7f8c8d;
  font-size: 18px;
  margin: 0;
}

.login-form {
  margin-top: 40px;
  width: 100%;
  max-width: 420px;
}

.login-input {
  margin-bottom: 25px;
}

.login-input :deep(.el-input__wrapper) {
  border-radius: 12px;
  border: 2px solid #e1e8ed;
  transition: all 0.3s ease;
  background: #f8f9fa;
  height: 55px;
}

.login-input :deep(.el-input__wrapper:hover) {
  border-color: #667eea;
  background: white;
}

.login-input :deep(.el-input__wrapper.is-focus) {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
  background: white;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  font-size: 16px;
}

.forgot-password {
  color: #667eea;
  text-decoration: none;
  transition: color 0.3s ease;
}

.forgot-password:hover {
  color: #5a6fd8;
}

.login-button {
  width: 100%;
  height: 55px;
  border-radius: 12px;
  font-size: 18px;
  font-weight: 600;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 15px 30px rgba(102, 126, 234, 0.3);
}

.login-button:active {
  transform: translateY(0);
}

.register-link {
  text-align: center;
  margin-top: 30px;
  font-size: 16px;
  color: #7f8c8d;
}

.link {
  color: #667eea;
  text-decoration: none;
  font-weight: 600;
  margin-left: 5px;
  transition: color 0.3s ease;
}

.link:hover {
  color: #5a6fd8;
}
@media (max-width: 1024px) {
  .login-card {
    padding: 40px 0 30px 0;
    max-width: 95vw;
  }
  .title {
    font-size: 32px;
  }
  .subtitle {
    font-size: 16px;
  }
  .logo {
    width: 70px;
    height: 70px;
    font-size: 28px;
  }
}
@media (max-width: 600px) {
  .login-card {
    padding: 20px 0 10px 0;
    max-width: 100vw;
  }
  .login-form {
    max-width: 100vw;
  }
}
@media (min-width: 1440px) {
  .login-card {
    padding: 70px 0 50px 0;
    max-width: 480px;
  }
  .title {
    font-size: 40px;
  }
  .subtitle {
    font-size: 20px;
  }
  .logo {
    width: 90px;
    height: 90px;
    font-size: 36px;
  }
}
@media (min-width: 1920px) {
  .login-card {
    padding: 80px 0 60px 0;
    max-width: 550px;
  }
  .title {
    font-size: 44px;
  }
  .subtitle {
    font-size: 22px;
  }
  .logo {
    width: 100px;
    height: 100px;
    font-size: 40px;
  }
  .login-button {
    height: 60px;
    font-size: 20px;
  }
  .login-input :deep(.el-input__wrapper) {
    height: 60px;
  }
}
</style>